package com.z.admin.controller;

import com.z.blog.entity.User;
import com.z.blog.service.IBlogService;
import com.z.blog.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class IndexController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IBlogService blogService;


    @GetMapping({"/login", "", "/"})
    public String index() {
        return "admin/login";
    }

    @PostMapping("/login")
    public String login(String username, String password, HttpSession session) {
        try {
            User user = userService.login(username, password);
            if(user != null){
                session.setAttribute("user", user);
                return "redirect:/admin/index";
            }
        }catch (Exception e ){
            e.printStackTrace();
        }
        return "admin/login";
    }


    @GetMapping("/index")
    public String index(HttpSession session) {
        return "admin/index";
    }
    @GetMapping("/type")
    public String type() {
        return "admin/type";
    }
    @GetMapping("/tag")
    public String tag() {
        return "admin/tag";
    }
}
